package com.falcon.modules.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.falcon.modules.domain.bo.MyPage;
import com.falcon.modules.domain.bo.UserSearchParam;
import com.falcon.modules.domain.dto.User;
import com.falcon.modules.domain.result.RestDataResult;
import com.falcon.modules.service.IUserService;
import com.falcon.util.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springside.modules.utils.mapper.JsonMapper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("list")
    public RestDataResult<User> list(MyPage<User> page, User user) {
        Page<User> userPage = userService.selectPage(page, new EntityWrapper<User>(user));
        return new RestDataResult<User>(userPage.getRecords(), userPage.getTotal());
    }

    @RequestMapping("edit")
    public boolean edit(User user) {
        user.setPwd(null);
        user.setCreateTime(null);
        return userService.updateById(user);
    }

    @RequestMapping("add")
    public boolean add(User user){
        user.setPwd(PasswordUtil.encrypt(user.getAccount()));
        return userService.insertOrUpdate(user);
    }

}
